import 'package:casdon/core/logger/log.dart';
import 'package:casdon/core/network/business_exception.dart';
import 'package:casdon/modules/recipe/data/api/recipe_user_info_api.dart';
import 'package:casdon/modules/recipe/data/repository/recipe_repository.dart';
import 'package:casdon/modules/recipe/providers/recipe_provider.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

class RecipeUserInfoUsecase {
  final RecipeRepository _repository;

  const RecipeUserInfoUsecase(this._repository);

  Future<RecipeUserInfoApi?> execute({required String menuId}) async {
    try {
      var ret = await _repository.getUserRecipeInfo(menuId);
      return ret;
    } on BusinessException {
      rethrow;
    } catch (e) {
      Log.e("RecipeTodayListUsecase 失败", e.toString(), StackTrace.current);
      throw Exception("RecipeTodayListUsecase 失败");
    }
  }
}

final recipeUserInfoUsecaseProvider = Provider(
  (ref) => RecipeUserInfoUsecase(ref.read(recipeRepository)),
);

final recipeUserInfoProvider = FutureProvider.family
    .autoDispose<RecipeUserInfoApi?, String>((ref, menuId) async {
      final usecase = ref.read(
        recipeUserInfoUsecaseProvider,
      ); // 你自己的 repository Provider

      return await usecase.execute(menuId: menuId);
    });
